package com.pet.server.ext.action.user;

import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.pet.server.ext.ActionBase;
import com.pet.server.ext.ApiError;
import com.pet.server.ext.Result;
import com.pet.server.ext.constants.BaseAttribute;
import com.pet.server.ext.data.PetInfo;
import com.pet.server.ext.data.UserInfo;
import com.pet.server.ext.service.UserService;
import com.pet.server.ext.util.CookieHelper;
import com.pet.server.ext.util.FileOperate;
import com.pet.server.ext.util.MD5Util;
import com.pet.server.ext.util.Parameters;
import com.pet.server.ext.util.StringUtil;
public class Bind extends ActionBase{
	public String userName,password,user_type,ico_path;
	public String nick_name,uuid,sex;
	public Bind(){
		 needCheckAuth =false;
	}

	@Override
	public void initParameters(Parameters params) throws ApiError {	
		//userName = params.getFieldValue("user_name");
		password = params.getFieldValue("password");
		user_type=params.getFieldValue("user_type");
		nick_name=params.getFieldValue("nick_name");
		ico_path=params.getFieldValue("ico_path");
		sex = params.getFieldValue("sex");
		uuid = params.getFieldValue("uuid");
		userName=StringUtil.createId(16);
		if("wechat".equals(user_type)){
			password = BaseAttribute.sns_wechat_pwd;
			if(StringUtil.isEmpty(uuid)){
				throw ApiError.Type.INVALID_PARAM.toException();
			}
		}else if("weibo".equals(user_type)){
			password = BaseAttribute.sns_weibo_pwd;
			if(StringUtil.isEmpty(uuid)){
				throw ApiError.Type.INVALID_PARAM.toException();
			}
		}else if("qq".equals(user_type)){
			password = BaseAttribute.sns_qq_pwd;
			if(StringUtil.isEmpty(uuid)){
				throw ApiError.Type.INVALID_PARAM.toException();
			}
		}else{
			throw ApiError.Type.INVALID_PARAM.toException();
		}
	}
	@Override
	public Result invokeService() throws ApiError {
		UserService userService = new UserService();
		UserInfo u = userService.findUserInfoBySnsSid(uuid);			
		if(u==null){
			UserInfo niu = userService.findUserInfoBynickName(nick_name);
			if(niu!=null){
				throw ApiError.Type.NICK_NAME_ERROR.toException("昵称 ["+nick_name+"]已经存在!");
			}
			u=new UserInfo();
			u.create_time=new Date();
			u.nick_name=nick_name;
			u.user_name=userName;
			u.sex=StringUtil.equals("female", sex)?"female":"male";
			u.password = MD5Util.MD5Encode(password, null);
			u.user_type=user_type;
			u.ico_path=ico_path;//FileOperate.uploadFile(ico_path);
			u.sns_sid=uuid;
			u = userService.saveUserInfo4Sns(u);
		}
		
		Result result = new Result();
		JSONObject o = new JSONObject();
		o.put("user_name", u.user_name);
		o.put("user_type", u.user_type);
		o.put("nick_name", u.nick_name);
		o.put("user_id", u.user_id);
		o.put("ico_path",u.ico_path);
		
        List<PetInfo> list = userService.queryPetInfoByUserId(u.user_id);
		JSONArray array = new JSONArray();
		for(PetInfo pet:list){
			JSONObject so = new JSONObject();
			so.put("pet_id", pet.pet_id);
			so.put("pet_name", pet.pet_name);
			so.put("ico_path", pet.ico_path);
			so.put("breed", pet.breed);
			so.put("cate", pet.pet_cate);
			so.put("birthday", pet.birthday.getTime()/1000);
			array.add(so);
		}
		o.accumulate("pet_item", array);
		String cookie = CookieHelper.createCookie(u);
		JSONObject data = new JSONObject();
		data.put("data", o);
		result.setRet(data, cookie);
	    return result;
	}
}